package com.cqupt.scanning.system.acl.controller;


import com.cqupt.scanning.system.acl.entity.dto.RoleGroupDTO;
import com.cqupt.scanning.system.acl.service.AclRoleGroupService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 宋宝梁
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/admin/acl/roleGroup")
public class AclRoleGroupController {

    private AclRoleGroupService aclRoleGroupService;

    @Autowired
    public AclRoleGroupController(AclRoleGroupService aclRoleGroupService) {
        this.aclRoleGroupService = aclRoleGroupService;
    }

    /**
     * 新增角色分组
     * @param roleGroupDTO
     * @return
     */
    @PostMapping("/addRoleGroup")
    @PreAuthorize("hasAuthority('roleGroup.addRoleGroup')")
    public R addRoleGroup(@RequestBody @Validated RoleGroupDTO roleGroupDTO) {
        return aclRoleGroupService.addRoleGroup(roleGroupDTO);
    }

    /**
     * 获取所有分组(不支持分页)
     * @return
     */
    @GetMapping("/getAllRoleGroup")
    @PreAuthorize("hasAuthority('roleGroup.getAllRoleGroup')")
    public R getAllRoleGroup() {
        return aclRoleGroupService.getAllRoleGroup();
    }

    /**
     * 修改角色组
     * @param roleGroupDTO
     * @return
     */
    @PutMapping("/updateRoleGroup")
    @PreAuthorize("hasAuthority('roleGroup.updateRoleGroup')")
    public R updateRoleGroup(@RequestBody @Validated RoleGroupDTO roleGroupDTO) {
        return aclRoleGroupService.updateRoleGroup(roleGroupDTO);
    }

    /**
     * 删除角色组（此操作会删除与角色组相关的角色）
     * @param roleGroupId
     * @return
     */
    @DeleteMapping("/removeRoleGroup/{id}")
    @PreAuthorize("hasAuthority('roleGroup.removeRoleGroup')")
    public R removeRoleGroup(@PathVariable("id") Integer roleGroupId) {
        return aclRoleGroupService.removeRoleGroup(roleGroupId);
    }

    /**
     * 获取角色分组及其角色
     * @return
     */
    @GetMapping("/getGroupAndRole")
    @PreAuthorize("hasAuthority('roleGroup.getGroupAndRole')")
    public R getGroupAndRole() {
        return aclRoleGroupService.getGroupAndRole();
    }
}

